package com.lehecai.core.lottery.content.impl;

import com.lehecai.core.exception.lottery.content.UnmatchedElementAttributeClassException;
import com.lehecai.core.exception.lottery.content.UnsupportedElementAttributeException;
import com.lehecai.core.lottery.content.IElementAttribute;
import com.lehecai.core.lottery.content.IElementAttributeParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * User: sunshow
 */
public class CommonElementAttributeParser implements IElementAttributeParser {

    protected final transient Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public IElementAttribute parse(String attribute) throws UnsupportedElementAttributeException {
        return new CommonElementAttribute(attribute);
    }

    @Override
    public <T extends IElementAttribute> T parse(String attribute, Class<T> clazz) throws UnsupportedElementAttributeException, UnmatchedElementAttributeClassException {
        IElementAttribute lotteryContentAttribute = this.parse(attribute);

        try {
            return clazz.cast(lotteryContentAttribute);
        } catch (ClassCastException e) {
            logger.error("类型转换出错", e);
            throw new UnmatchedElementAttributeClassException(e.getMessage());
        }
    }
}

